<template>
  <div>
    <div class="warn-top">
      <h4 style="text-align: left; color: #126bff">考勤组管理</h4>
      <!--         :rules="rules" -->
      <el-form
        ref="elForm"
        :model="formData"
        size="medium"
        label-width="80px"
        style="display: flex"
      >
        <el-form-item label="考勤组名称:" prop="name" style="width: 450px">
          <el-input
            v-model="formData.name"
            placeholder="请输入考勤组名称"
            clearable
            :style="{ width: '80%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item size="small " style="position: relative; right: 20px">
          <el-button @click="resetForm()" id="reset">重置</el-button>
          <el-button
            type="primary"
            @click="submitForm(formData)"
            id="submitForm"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="warn-content" style="width: 100%">
      <div>
        <el-button
          icon="el-icon-plus"
          @click="addTeam"
          style="
            height: 36px;
            background-color: #126bff;
            border-radius: 6px;
            opacity: 1;
            color: #ffffff;
            font-size: 14px;
            font-family: Microsoft YaHei-Regular, Microsoft YaHei;
            font-weight: 400;
            margin-left: 10px;
          "
          >添加考勤组</el-button
        >
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        style="width: 100%; cursor: pointer"
      >
        <el-table-column prop="id" label="序号" width="100"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="time" label="考勤时间">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.time" :key="index">
              <span>
                {{ item.shift_id == 0 ? item.day + "休息" : item.day + "上班" }}
              </span>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="考勤组状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#126BFF"
              inactive-color="#FF4444"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="2"
              @change="changeRole(scope.row)"
            >
              <!-- changeRole -->
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button @click="editClick(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button
                style="color: #ff4444"
                @click="deletePeople(scope.row)"
                type="text"
                size="small"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[20, 40, 60]"
        :page-size="pageSize"
        layout="total, prev, pager, next, sizes"
        :total="total"
        style="float: right; margin-top: 20px"
      >
      </el-pagination>
    </div>

    <Delete
      :title="DeleteTitle"
      :dialogVisible="dialogVisible"
      @deleteConfirm="deleteConfirm"
      @handel="handel"
    ></Delete>

    <!-- <router-view></router-view> -->
  </div>
</template>
<script>
import {
  checkInList,
  qidongCheck,
  delCheck,
  deleteCheck,
} from "@/api/CheckAttendance";
import Delete from "@/components/dialog/delete.vue";
function getDayOfWeek(dayNumber) {
  let day;
  switch (dayNumber) {
    case 1:
      day = "星期一";
      break;
    case 2:
      day = "星期二";
      break;
    case 3:
      day = "星期三";
      break;
    case 4:
      day = "星期四";
      break;
    case 5:
      day = "星期五";
      break;
    case 6:
      day = "星期六";
      break;
    case 7:
      day = "星期日";
      break;
    default:
      day = "无效的日期";
  }
  return day;
}
export default {
  name: "WARN",
  components: { Delete },
  data() {
    return {
      dialogVisible: false,
      DeleteTitle: "你确定要删除该考勤组吗？",
      currentPage4: 4,
      // 按页数查询数据
      currentPage: 1,
      pageSize: 20,
      formData: {
        name: "",
      },
      total: null,
      detaile: {},
      tableData: [],
      initTableData: [], // 数据接收
      pamas: {
        token: JSON.parse(localStorage.getItem("userInfo")).token,
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        name: "",
        page: 1,
        pagesize: 20,
      },
      id: "",
    };
  },
  watch: {
    $route(to, from) {
      console.log(to, from);
    },
  },
  created() {},
  mounted() {
    // this.changeRole();
    this.init();
  },

  methods: {
    init() {
      let loadingInstance = this.$Loading.service({
        target: document.querySelector(".el-table__body-wrapper"),
      });
      checkInList(this.pamas).then((res) => {
        if (res.code == 200) {
          res.data.res.forEach((item) => {
            if (item.time.length >= 0) {
              item.time.forEach((items) => {
                // console.log(items);
                items.day = getDayOfWeek(items.day);
              });
            }
          });
          loadingInstance.close();
          this.total = res.data.count;
          // console.log(res.data.res);
          this.tableData = res.data.res.reverse();
        }
      });
    },
    // 查询信息
    submitForm(formData) {
      this.pamas.name = formData.name;
      console.log(this.pamas);
      this.init();
    },
    // 查询重置  // 重新调用
    resetForm() {
      this.$refs["elForm"].resetFields();
      this.pamas = {
        token: JSON.parse(localStorage.getItem("userInfo")).token,
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        name: "",
        page: 1,
        pagesize: 20,
      };
      this.init();
    },
    // 添加考勤组
    addTeam() {
      this.$router.push("addAttendance");
      // this.$router.go("addAttendance");
    },
    // 编辑考勤组
    editClick(row) {
      console.log(row);
      this.$router.push({
        path: "addAttendance",
        query: {
          query: JSON.stringify(row),
        },
      });
    },
    changeRole(row) {
      console.log(row);
      if (row.status == 1) {
        qidongCheck({
          token: JSON.parse(localStorage.getItem("userInfo")).token,
          ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
          id: row.id,
        }).then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success(res.message);
            this.init();
          }
        });
      } else {
        delCheck({
          id: this.id,
        }).then((res) => {
          if (res.code == 200) {
            console.log(res);
            this.$message.success(res.message);
            this.init();
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },
    // 删除考勤组
    deleteConfirm() {
      console.log(this.id);
      deleteCheck({
        id: this.id,
      }).then((res) => {
        if (res.code == 200) {
          console.log(res);
          this.$message.success(res.message);
          this.init();
        } else {
          this.$message.error(res.message);
        }
      });
      this.dialogVisible = false;
      console.log("删除");
    },
    // 取消删除
    handel() {
      this.dialogVisible = false;
      console.log("取消");
    },
    deletePeople(e) {
      this.dialogVisible = true;
      console.log(e);
      this.id = e.id;
      // delCheck()
    },
    // 分页器相关操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.pamas.pagesize = val;
      this.pamas.page = 1;
      this.currentPage = 1;
      this.init();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.pamas.page = val;
      this.init();
    },
  },
};
</script>
<style lang="scss" scoped>
.warn-top {
  // border: 1px red solid;
  padding: 10px;
  background-color: white;
  border-radius: 10px;
  width: 100%;
  height: 120px;
  box-sizing: border-box;
}
.warn-top {
  width: calc(100%);
}
/deep/.el-form {
  display: flex;
  height: 80px;
  align-items: center;
}
.el-table {
  width: calc(100%);
}
/deep/.el-form-item__label {
  // border: 1px red solid;
  width: 90px !important;
}
/deep/.el-input--medium {
  position: relative !important;
  right: 30px !important;
}
/deep/.el-range-editor--medium .el-range-separator {
  line-height: 21px;
  font-size: 14px;
}
/deep/ .el-range__icon {
  line-height: 21px;
}
/deep/.el-input--medium .el-input__icon {
  line-height: 44px !important;
}
// /deep/.el-form-item__content {
//   border: 1px red solid;
// }
.warn-content {
  margin-top: 20px;
  // border: 1px red solid;
  height: auto;
  background-color: white;
  border-radius: 10px;
  padding: 10px;
  box-sizing: border-box;
  overflow: auto;
}
/deep/.warn-content > div:nth-of-type(1) {
  display: block !important;
  padding: 10px 0 10px 0;
  text-align: left;
  // border: 1px red solid;
}
/deep/.el-dialog {
  border-radius: 15px !important;
  height: auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
/deep/.el-dialog__header {
  text-align: left;
  font-weight: 600;
}
.warn-content > div:nth-of-type(1) > button:nth-of-type(1) {
  font-size: small;
  width: 108px;
  height: 36px;
  background: #126bff;
  border-radius: 8px 8px 8px 8px;
  color: #ffffff;
  display: flex;
  /* flex-direction: row; */
  justify-content: center;
}
.warn-content > div:nth-of-type(1) > button:nth-of-type(2) {
  font-size: small;
  color: #ffffff;
  width: 108px;
  height: 36px;
  background: #ff4444;
  border-radius: 8px 8px 8px 8px;
}
.el-table .el-table__cell {
  padding: 12px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: center;
}
.upIpfo {
  display: flex;
  width: 100%;
  // border: 1px red solid;
  justify-content: space-between;
}
.upIpfo p {
  color: #13213b;
  font-size: 14px;
  font-weight: 400;
  width: 50%;
  margin-top: 10px;
  // border: 1px red solid;
  display: inline-block;
  // background-color: skyblue;
}
/deep/.el-table__header-wrapper {
  // border: 1px red solid !important;
  border-radius: 10px 10px 0 0;
}
/deep/.el-table {
  border: 1px #d7dbe3 solid;
  border-radius: 10px;
}
/deep/.el-input--medium .el-input__icon {
  line-height: 30px;
}
/deep/.has-gutter tr th {
  color: black;
  text-align: center;
  border: 1px d7dbe3 solid;
  background-color: #ecf5ff;
}

/deep/.is-leaf {
  border: 1px #d7dbe3 solid !important;
  border-top: none !important;
  border-left: none !important;
}
/deep/.el-table__cell {
  text-align: center !important;
}
/deep/.el-dialog {
  border-radius: 15px !important;
}
/deep/.el-dialog__header {
  text-align: left;
  font-weight: 600;
}

/deep/.el-input__inner {
  border: 1px solid #d7dbe3 !important;
  background-color: white !important;
  border-radius: 6px !important;
}
::v-deep.el-table th.el-table__cell {
  background-color: #ecf5ff !important;
  width: 64px !important;
  height: 21px !important;
  font-size: 16px !important;
  color: #13213b !important;
}
</style>

